package com.lzy.shops.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzy.shops.bean.GoodsType;
import com.lzy.shops.bean.PageBean;
import com.lzy.shops.service.GoodsTypeService;
import com.lzy.shops.utils.PageBeanUtils;
import com.lzy.shops.utils.ResponseData;
import com.lzy.shops.utils.exception.ActionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/system/goodsType")
public class GoodsTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/findAll")
    public String findAll(@RequestParam Map<String,Object> maps) throws JsonProcessingException {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        String allType = valueOperations.get("allType");
        Integer pageSize = null;
        Integer pageNo = null;
        Integer start = null;
        List pageList = new ArrayList();
        String page = (String) maps.get("page");
        if(page==null && !"no".equals(page)){
            if(!maps.isEmpty()){
                pageSize = Integer.parseInt((String) maps.get("pageSize"));
                pageNo = Integer.parseInt((String) maps.get("pageNo"));
                start = pageNo >= 1 ? (pageNo - 1) * pageSize : 1;
            }
            if(allType == null||allType.equals("")){
                try {
                    pageList = goodsTypeService.findAll(0);
                    if(pageList != null){
                        String types = objectMapper.writeValueAsString(new ResponseData<List<GoodsType>>(true, pageList, "商品类型"));
                        valueOperations.set("allType",types);
                        return types;
                    }
                } catch (ActionException e) {
                    pageList = null;
                }
            }else{
                ResponseData responseData = JSONObject.parseObject(allType, ResponseData.class);
                pageList = (List) responseData.getData();
            }
            if(pageSize!=null&&pageNo!=null){
                PageBean run = PageBeanUtils.run(pageList, start, pageSize);
                run.setPageNo(pageNo);
                return objectMapper.writeValueAsString(new ResponseData<PageBean>(true,run));
            }else{
                PageBean run = new PageBean(100,1,pageList,pageList.size());
                return objectMapper.writeValueAsString(new ResponseData<PageBean>(true,run));
            }
        }else{
            try {
                List<GoodsType> all = goodsTypeService.findAll(0);
                return objectMapper.writeValueAsString(new ResponseData<List>(true,all));
            } catch (ActionException e) {
                return objectMapper.writeValueAsString(new ResponseData<List>(false,"数据异常"));
            }
        }
    }

    @PostMapping("/addGoodsType")
    public String addGoodsType(@RequestBody GoodsType type) throws JsonProcessingException {
        Integer integer = 0;
        ResponseData<GoodsType> responseData = null;
        try {
            integer = goodsTypeService.addGoodsType(type);
            if(integer==1){
                responseData = new ResponseData<GoodsType>(true,"添加成功");
                redisTemplate.delete("allType");
            }
        } catch (Exception e) {
            responseData = new ResponseData<GoodsType>(false,"添加失败");
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @PutMapping("/updateGoodsType")
    public String updateGoodsType(@RequestBody GoodsType goodsType) throws JsonProcessingException {
        Integer integer = 0;
        ResponseData<GoodsType> responseData = null;
        try {
            integer = goodsTypeService.updateGoodsType(goodsType);
            if(integer==1){
                responseData = new ResponseData<GoodsType>(true,"修改成功");
                redisTemplate.delete("allType");
            }
        } catch (Exception e) {
            responseData = new ResponseData<GoodsType>(false,"修改失败");
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @DeleteMapping("/delGoodsType/{typeId}")
    public String delGoodsType(@PathVariable("typeId") Integer id) throws JsonProcessingException {
        ResponseData<Map<String, String>> responseData = null;
        Map<String, String> info = new HashMap<>();
        try {
            info = goodsTypeService.delGoodsType(id);
            if(info.size()>0){
                responseData = new ResponseData<Map<String, String>>(true,info);
                redisTemplate.delete("allType");
            }
        } catch (Exception e) {
            responseData = new ResponseData<Map<String, String>>(false,"操作失败");
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @GetMapping("/getDefaultData")
    public String getDefaultData() throws JsonProcessingException {
        ResponseData<Object> responseData;
        try {
            List<GoodsType> goodsTypes = goodsTypeService.defaultData();
            responseData = new ResponseData<Object>(true,goodsTypes);
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,new ArrayList<>());
        }
        return objectMapper.writeValueAsString(responseData);
    }
}
